import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class HttpPage extends StatefulWidget {
  @override
  _HttpPageState createState() => _HttpPageState();
}

class _HttpPageState extends State<HttpPage> {
  String showResult = '';

  String url = 'http://www.devio.org/io/flutter_app/json/test_common_model.json';

  Future <CommonModel> fetchPost() async{
    final response = await http.get(url);
    final result = json.decode(response.body);
    return CommonModel.fromJson(result);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Http'),),
        body: Column(
          children: <Widget>[
            InkWell(
              onTap: (){
                fetchPost().then((CommonModel value) {
                  setState(() {
                    showResult = '请求结果：\n hideAppBar:${value.hideAppBar}'
                        '\n icon: ${value.icon}';
                  });
                });
              },
              child: Text('点我',
                style: TextStyle(fontSize: 26),),
            ),
            Text(showResult),
          ],
        ),
      ),
    );
  }
}

class CommonModel{
  final String icon;
  final String title;
  final String url;
  final String statusBarColor;
  final bool hideAppBar;

  CommonModel({this.icon, this.title, this.url,
    this.statusBarColor, this.hideAppBar});

  factory CommonModel.fromJson(Map<String,dynamic> json){
    return CommonModel(
      icon: json['icon'],
      title: json['title'],
      url: json['url'],
      statusBarColor: json['statusBarColor'],
      hideAppBar: json['hideAppBar'],
    );
  }
}
